package com.fy.market.strategy.service.rule.chain.impl;

import com.fy.market.strategy.model.entity.RuleChainResultEntity;
import com.fy.market.strategy.model.entity.RuleMatterEntity;
import com.fy.market.strategy.model.entity.StrategyRuleBlacklistEntity;
import com.fy.market.strategy.repository.IStrategyRepository;
import com.fy.market.strategy.service.rule.chain.AbstractLogicChain;
import com.fy.market.types.common.utils.FyObjUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * @author fwq
 * @date 2025-09-18
 * @desc 责任链黑名单节点
 */
@Slf4j
@Component("rule_blacklist")
public class BlackLogicChain extends AbstractLogicChain {

    @Resource
    private IStrategyRepository iStrategyRepository;

    /**
     * 黑名单过滤
     *
     * @param userId     用户ID
     * @param strategyId 策略ID
     * @return 奖品ID
     */
    @Override
    public RuleChainResultEntity logic(Integer userId, Integer strategyId) {


        log.info("----- 开始执行黑名单过滤(前置) -----");
        RuleMatterEntity ruleMatterEntity = new RuleMatterEntity();
        ruleMatterEntity.setUserId(userId);
        ruleMatterEntity.setStrategyId(strategyId);
        StrategyRuleBlacklistEntity strategyRuleBlacklistEntity = iStrategyRepository.queryBlackList(ruleMatterEntity);

        if (FyObjUtil.isNullObj(strategyRuleBlacklistEntity)) {
            log.info("当前用户不在黑名单中,交给下一个责任链");
            return next().logic(userId, strategyId);
        }

        log.info("黑名单过滤接管,获得兜底奖品:{}",strategyRuleBlacklistEntity.getAwardId());
        return RuleChainResultEntity.builder().guaranteedAwardId(strategyRuleBlacklistEntity.getAwardId()).strategyId(strategyId).build();
    }
}
